<?php
/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/

?>
<?php
class Admin_ChannelController extends Zend_Controller_Action{
	public function init(){
		if( !At_User::getInstance()->hasIdentity() ){
			$this->_redirect('/admin/user/login');
			exit;
		}

		$this->_helper->layout->setLayout('admin');
	}

	// default action: forward to list
	public function indexAction(){
		$this->_forward( 'list' );
	}

	/**
	 * list all channels
	 */
	public function listAction(){
		$channel = new Default_Model_Channel();

		$this->view->channel  = $channel;
		$this->view->channels = $channel->fetchAll();
	}

	/**
	 * add a new channel
	 */
	public function addAction(){
		$request = $this->getRequest();
		$channel = new Default_Model_Channel();
		$form    = new Admin_Form_Channel( $channel );

		if( $request->isPost() && $form->isValid( $request->getPost() ) ){
			$values	  = $form->getValues();
			unset( $values['save'] );

			foreach( $values as $name => $value ){
				$channel->$name = $value;
			}

			$channel->save();
			$this->_redirect( '/admin/channel/edit/' . $channel->id );
		}

		$this->view->channel = $channel;
		$this->view->form	 = $form;
	}

	/**
	 * edit a channel
	 */
	public function editAction(){
		$request = $this->getRequest();

		$channel = new Default_Model_Channel( $request->id );
		$form    = new Admin_Form_Channel( $channel );

		if( $request->isPost() && $form->isValid( $request->getPost() ) ){
			$values	  = $form->getValues();
			$keywords = (object) $values['keywords'];
			unset( $values['save'] );
			unset( $values['keywords'] );

			foreach( $values as $name => $value ){
				$channel->$name = $value;
			}


			$current = $channel->getKeywords();

			if( null !== $keywords->selected ){
				foreach( $current as $index => $kw ){
					if( in_array( $kw->id, $keywords->selected ) ){
						unset( $current[$index] );
					}
				}
			}
			if( ! empty($keywords->keyword_name) ){
				$keyword = new Default_Model_Keyword(array(
					'domain_id'	=> $keywords->keyword_domain,
					'name'		=> $keywords->keyword_name
				));

				$keyword->save();
				$current[] = $keyword;
			}

			$channel->setKeywords( $current );

			$channel->save();

            if( isset( $request->keywords->update ) ){
                $this->_redirect( '/admin/channel/edit/' . $channel->id );
            }

			$this->_redirect( '/admin/channel/list');
		}

		$this->view->channel = $channel;
		$this->view->form	 = $form;
	}

	/**
	 * delete a channel
	 */
	public function deleteAction(){
		$request = $this->getRequest();
		$channel = new Default_Model_Channel( $request->id );
		$form	 = new Zend_Form;

		$form->addElement('submit', 'delete', array('label'	=> 'Delete' ));
		$form->addElement('submit', 'cancel', array('label'	=> 'Cancel' ));

		foreach( $form->getElements() as $element ){
			$element->removeDecorator('DtDdWrapper');
			$element->removeDecorator('Label');
		}
		$form->removeDecorator('HtmlTag');

		if( $this->getRequest()->isPost() && $form->isValid( $this->getRequest()->getPost() ) ){
			if( $request->delete !== null ){
				$channel->delete();
			}

			$this->_redirect('/admin/channel/list');
		}

		$this->view->form 		= $form;
		$this->view->channel	= $channel;
	}
}
